﻿using DevExpress.XtraEditors;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using NPOI.SS.Formula.Functions;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using static System.Windows.Forms.Control;

namespace Barcode_Project.Local
{
    internal static class CommonMethods
    {
        /// <summary>
        /// 表单赋值
        /// </summary>
        /// <param name="winControl">表单容器</param>
        /// <param name="datas">数据</param>
        /// <param name="qzName">前缀名</param>
        public static void SetFormValue(ControlCollection winControl,Dictionary<string,string> datas,string qzName)
        {
            foreach (Control control in winControl)
            {
                if (control is TextEdit)
                {
                    var kjName = control.Name.Replace(qzName, "");
                    foreach (var d in datas)
                    {
                        if (d.Key == kjName)
                        {
                            control.Text = d.Value;
                            break;
                        }
                    }
                }
            }
        }
        /// <summary>
        /// 将某个实体对象转换成表格(空表)
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        private static DataTable DictionaryToTable(Dictionary<string,string> obj)
        {
            DataTable table= new DataTable();
            foreach (var d in obj)
            {
                if (!table.Columns.Contains(d.Key))
                {
                    table.Columns.Add(d.Key);
                }
            }
            if (!table.Columns.Contains("barCode"))
            {
                table.Columns.Add("barCode");
            }
            return table;
        }
      /// <summary>
      /// 创建打印表格
      /// </summary>
      /// <param name="obj"></param>
      /// <param name="bars"></param>
      /// <returns></returns>
        public static DataTable CreatePrintTableByDictionary(Dictionary<string,string> obj, string bars,string info="")
        {
            var table = DictionaryToTable(obj);
            var barArr = bars.Split(',');
            foreach (var bar in barArr)
            {
                if (!string.IsNullOrWhiteSpace(bar))
                {
                    DataRow r = table.NewRow();
                    r["barCode"] = bar;
                    foreach (var d in obj)
                    {
                        if (table.Columns.Contains(d.Key) && !string.IsNullOrWhiteSpace(d.Value))
                        {
                            r[d.Key] = d.Value;
                        }
                    }
                    table.Rows.Add(r);
                }
            }
            if (!string.IsNullOrWhiteSpace(info))
            {
                var list = JsonConvert.DeserializeObject<List<JObject>>(info);
                foreach (var obj_b in list[0])
                {
                    if (!table.Columns.Contains(obj_b.Key))
                    {
                        table.Columns.Add(obj_b.Key);
                    }
                }
                foreach (var obj_b in list)
                {
                    foreach (DataRow r in table.Rows)
                    {
                        if (obj_b["barCode"].ToString() == r["barCode"].ToString())
                        {
                            r.BeginEdit();
                            foreach (var obj_p in obj_b)
                            {
                                if (r[obj_p.Key.ToString()]==null || string.IsNullOrWhiteSpace(r[obj_p.Key.ToString()].ToString()))
                                {
                                    if (obj_p.Value != null && !string.IsNullOrWhiteSpace(obj_p.Value.ToString()))
                                    {
                                        r[obj_p.Key.ToString()] = obj_p.Value.ToString();
                                    }
                                }
                            }
                            r.EndEdit();
                            break;
                        }
                    }


                   

                }
               
            }
            return table;
        }
        /// <summary>
        /// 将表单容器内的全部值转变成指定对象
        /// </summary>
        /// <param name="dic"></param>
        /// <param name="winControl"></param>
        /// <param name="qzName"></param>
        /// <returns></returns>
        public static void SetParameterValue(ref Dictionary<string,string> dic, ControlCollection winControl, string qzName)
        {
            foreach (Control control in winControl)
            {
               
                if (control is TextEdit)
                {
                    var kjName = control.Name.Replace(qzName, "");
                    string value = control.Text;
                    if (!string.IsNullOrWhiteSpace(value))
                    {
                        if (!dic.ContainsKey(kjName) || string.IsNullOrWhiteSpace(dic[kjName]))
                        {
                            dic[kjName] = value;
                        }
                        else if(dic.ContainsKey(kjName) && !string.IsNullOrWhiteSpace(dic[kjName]))
                        {
                            dic[kjName] = value;
                        }
                    }
                }
                else if (control is System.Windows.Forms.ComboBox)
                {
                    var kjName = control.Name.Replace("cb_", "");
                    var kj = (System.Windows.Forms.ComboBox)control;
                    var value = kj.SelectedValue;
                    if (value != null && !string.IsNullOrWhiteSpace(value.ToString()))
                    {
                        if (!dic.ContainsKey(kjName) || string.IsNullOrWhiteSpace(dic[kjName]))
                        {
                            dic[kjName] = value.ToString();
                        }
                        else if (dic.ContainsKey(kjName) && !string.IsNullOrWhiteSpace(dic[kjName]))
                        {
                            dic[kjName] = value.ToString();
                        }
                    }
                }
            }
        }
        /// <summary>
        /// 根据窗体参数对实体对象进行赋值(暂时不用)
        /// </summary>
        /// <param name="obj"></param>
        /// <param name="dic"></param>
        /// <returns></returns>
        public static object SetObjectHiddenValue(object obj,Dictionary<string,string> dic)
        {
            var objectType = obj.GetType();
            PropertyInfo[] propertys = objectType.GetProperties();
            foreach (PropertyInfo pi in propertys)
            {
                var value = pi.GetValue(obj, null);
                if (value == null || string.IsNullOrWhiteSpace(value.ToString()))
                {
                    if (dic.ContainsKey(pi.Name.Trim()))
                    {
                        pi.SetValue(obj, dic[pi.Name.Trim()]);
                    }
                }
            }
            return obj;
        }
        /// <summary>
        /// 接口地址
        /// </summary>
        /// <returns></returns>
        public static string APIUrl()
        {
           return ConfigurationManager.AppSettings["APIUrl"].ToString();
        }
    }
}
